package com.ruoyi.business.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.domain.LoanOverflow;
import com.ruoyi.bizsys.dto.UserInfo;
import com.ruoyi.bizsys.service.IAppUserService;
import com.ruoyi.bizsys.service.ILoanOverflowService;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.business.common.AsyncService;
import com.ruoyi.business.domain.TransferVo;
import com.ruoyi.business.domain.WalletAccountReq;
import com.ruoyi.business.service.IRepayToWalletService;
import com.ruoyi.business.service.IWalletService;
import com.ruoyi.common.constant.WalletConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sms.domain.SmsSendData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @Author: jamnsin
 * @Date: 2022/7/22 17:29
 */
@Slf4j
@Service
public class RepayToWalletServicImpl implements IRepayToWalletService {

    @Autowired
    IWalletService walletService;

    @Autowired
    ILoanOverflowService loanOverflowService;

    @Autowired
    AsyncService asyncService;

    @Autowired
    IAppUserService appUserService;

    @Override
    public void addOverflowAndPushWallet(TransferVo transferVo) {
        log.info("开始创建溢缴款记录&推送钱包 >>> transferVo = {}", transferVo);

        addOverflowIncomeAndExpense(transferVo);

        walletService.updateWalletAccount(getWalletAccountReq(transferVo));

        if (StringUtils.equalsAny(transferVo.getActionType(), BizConstants.OVERFLOW_TYPE_MONNIFY_CHARGE, BizConstants.OVERFLOW_TYPE_PAYSTACK_CHARGE)) {
            log.info("钱包充值发送短信");

            UserInfo userInfo = appUserService.getUserInfoByCustNo(transferVo.getCustNo());

            JSONObject smsParam = new JSONObject();
            smsParam.put("lastName", userInfo.getLastName());
            smsParam.put("amount", transferVo.getTransferAmt().setScale(2, BigDecimal.ROUND_UP));
            SmsSendData smsData = SmsSendData.builder().noticeType(BizConstants.SMS_NOTICE_TYPE_NOTICE).smsType(BizConstants.SMS_TYPE_CHARGE_TO_WALLET).phoneNumber(userInfo.getRegisterPhone()).countryCode(userInfo.getCountryCode()).smsParam(smsParam).build();
            asyncService.sendSms(smsData);
        }

        log.info("结束创建溢缴款记录&推送钱包 >>>");
    }

    private WalletAccountReq getWalletAccountReq(TransferVo transferVo) {

        String fundsType = "";
        if (StringUtils.equalsAny(transferVo.getActionType(), BizConstants.OVERFLOW_TYPE_MONNIFY_CHARGE, BizConstants.OVERFLOW_TYPE_FINANCE_CAL)) {
            fundsType = WalletConstants.WALLET_FLOW_FUNDS_TYPE_MONNIFY_CHARGE;
        } else if (StringUtils.equals(BizConstants.OVERFLOW_TYPE_PAYSTACK_CHARGE, transferVo.getActionType())) {
            fundsType = WalletConstants.WALLET_FLOW_FUNDS_TYPE_PAYSTACK_CHARGE;
        } else {
            fundsType = WalletConstants.WALLET_FLOW_FUNDS_TYPE_OVERFLOW;
        }

        return WalletAccountReq.builder()
                .amount(transferVo.getTransferAmt())
                .custNo(transferVo.getCustNo())
                .payTime(transferVo.getPayTime())
                .reference(transferVo.getReference())
                .fundsType(fundsType)
                .build();
    }

    private void addOverflowIncomeAndExpense(TransferVo transferVo) {
        saveOverflow(buildOverflowIncome(transferVo));
        saveOverflow(buildOverflowExpense(transferVo));
    }

    private void saveOverflow(LoanOverflow overflow) {
        log.debug("创建溢缴款记录，changeType = {}", overflow.getChangeType());
        loanOverflowService.insertLoanOverflow(overflow);
    }

    private LoanOverflow buildOverflowExpense(TransferVo transferVo) {
        LoanOverflow loanOverflow = new LoanOverflow();
        loanOverflow.setChangeAmount(transferVo.getTransferAmt().negate());
        loanOverflow.setChangeType(BizConstants.OVERFLOW_CHANGE_TYPE_EXPENSE);
        loanOverflow.setCustNo(transferVo.getCustNo());
        loanOverflow.setType(WalletConstants.OVERFLOW_TO_WALLET);
        loanOverflow.setTransactionReference(transferVo.reference);
        return loanOverflow;
    }

    private LoanOverflow buildOverflowIncome(TransferVo transferVo) {
        LoanOverflow loanOverflow = new LoanOverflow();
        loanOverflow.setChangeAmount(transferVo.getTransferAmt());
        loanOverflow.setChangeType(BizConstants.OVERFLOW_CHANGE_TYPE_INCOME);
        loanOverflow.setCustNo(transferVo.getCustNo());
        loanOverflow.setOverflowTime(transferVo.getPayTime());
        loanOverflow.setType(transferVo.getActionType());
        loanOverflow.setRepaymentOrderNo(transferVo.getRepaymentNo());
        loanOverflow.setTransactionReference(transferVo.getReference());
        return loanOverflow;
    }
}
